package com.zhilianbao.leyaogo.http.callback;

import android.content.Context;

import com.bql.utils.CheckUtils;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.app.LeYaoGoApplication;
import com.zhilianbao.leyaogo.view.dialog.LeProgressDialog;
import com.zhilianbao.okhttputils.request.BaseRequest;

/**
 * ClassName: UploadMultiFileCallback <br>
 * Description: 上传多个文件回调<br>
 * Author: Cyarie <br>
 * Created: 2016/8/8 16:36 <br>
 * Update Time：<br>
 * Update Description：<br>
 */
public abstract class UploadMultiFileCallback<T> extends JsonCallback<T> {

    private LeProgressDialog mProgressDialog;//进度对话框


    public UploadMultiFileCallback(Context context) {
        super(context);
        initPro(context);
    }


    /**
     * 初始化进度条对话框
     *
     * @param context
     */
    public void initPro(Context context) {
        if (mProgressDialog == null)
            mProgressDialog = new LeProgressDialog(context);
        mProgressDialog.setCanceledOnTouchOutside(false);
    }

    /**
     * 显示进度对话框
     *
     * @param text 显示文本
     */
    public void showPro(String text) {
        if (mProgressDialog != null && !mProgressDialog.isShowing()) {
            mProgressDialog.show();
        }

        if (!CheckUtils.isEmpty(text))
            mProgressDialog.showText(text);
    }

    /**
     * dismiss进度对话框
     */
    public void dismissPro() {
        if (mProgressDialog != null && mProgressDialog.isShowing())
            mProgressDialog.dismiss();
    }

    @Override
    public void onBefore(BaseRequest request) {
        super.onBefore(request);
        showPro(LeYaoGoApplication.getContext().getString(R.string.uploading_please_wait, "0%"));

    }

    @Override
    public void onAfter(T t, Exception e) {
        super.onAfter(t, e);
        dismissPro();
    }

    @Override
    public void upProgress(long currentSize, long totalSize, float progress, long networkSpeed) {
        super.upProgress(currentSize, totalSize, progress, networkSpeed);
        showPro(LeYaoGoApplication.getContext().getString(R.string.uploading_please_wait, (int) (100 * progress) + "%"));
    }

    //    @Override
    //    public UploadFileResponse convertSuccess(Response response) throws Exception {
    //        String body = response.body().string();
    //        CLog.e("LogInterceptor", body);
    //        UploadFileResponse uploadFileResponse = new Gson().fromJson(body, UploadFileResponse.class);
    //        if (uploadFileResponse == null) {
    //            //解析错误
    //            return null;
    //        } else {
    //            return uploadFileResponse;
    //
    //        }
    //    }
}
